﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace PipeTest
{
    public partial class TimerUserControl : UserControl
    {
        private Timer timer;

        public delegate void TickHandler(string key);

        public event TickHandler Tick;

        public TimerUserControl()
        {
            InitializeComponent();
        }

        private void EnabledCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            if(EnabledCheckBox.Checked)
            {
                timer=new Timer();
                timer.Interval = GetInterval();
                timer.Tick += new EventHandler(TimerTick);
                timer.Start();
            }
            else
            {
                timer.Stop();
                timer.Dispose();
            }
        }

        private void TimerTick(object sender, EventArgs e)
        {
            if(Tick!=null)
            {
                Tick(KeysTextBox.Text.Trim());
            }
        }

        private int GetInterval()
        {
            int qty;
            if(!int.TryParse(IntervalTextBox.Text.Trim(),out qty))
            {
                qty = 30;
            }
            int weight=0;
            if("秒".Equals(DaneiComboBox.SelectedText))
            {
                weight = 1;
            }
            else if("分".Equals(DaneiComboBox.SelectedText))
            {
                weight = 60;
            }
            else if("时".Equals(DaneiComboBox.SelectedText))
            {
                weight = 3600;
            }else
            {
                weight = 1;
            }
            int x= qty*weight;
            if (x == 0) x = 1;
            return x*1000;
        }
    }
}
